<?php
session_start();
?>
<script language="javascript" src='scripts/dialog_box.js'>
</script>
<?
require_once ($_SERVER['DOCUMENT_ROOT']."/control/Utils.php");
require_once ($_SERVER['DOCUMENT_ROOT']."/control/DateUtils.php");
require_once ($_SERVER['DOCUMENT_ROOT']."/dao/dbConnection.php");
$connectionstring = ConnectMainDatabase();

addDeleteTruageListener();
addRegisterTruageListener();
addChangePasswordListener();
/*
 function editct($id,$manv,$tunam,$dennam,$congty)
 {
 global $connectionstring;
 $response = "<center>	<p align=center class=normal ><b>Chá»‰nh sá»­a QTCT</b>
 <form action=main_details.php method=post name=ct>
 <table width=380px border=1 cellpadding=2 cellspacing=0
 style=\'border-collapse: collapse\' bordercolor=#C0C0C0>
 <input type=hidden name=chinhsuact value=chinhsuact />
 <input type=hidden name=id value=$id />
 <input type=hidden name=manv value=$manv />
 <tr>
 <td class=formfield bgcolor=#DBDBDB ><b>Tá»« nÄƒm</b></td>
 <td ><input type=text class=formfield name=tunam size=20 maxlength=50 value=$tunam /></td>
 <td class=formfield bgcolor=#DBDBDB ><b>Ä�áº¿n nÄƒm</b></td>
 <td ><input type=text class=formfield name=dennam size=20 maxlength=50 value=$dennam />
 </td>
 </tr>
 <tr>
 <td class=formfield bgcolor=#DBDBDB><b>CÃ´ng ty</b>
 </td>
 <td colspan=3>
 <input type=text class=formfield name=congty size=50 maxlength=60 value=$congty />
 </td>
 </tr>
 </table>
 <br>
 <center><input type=submit value=\'Cáº­p nháº­t\'
 onclick=\'javascript: return checkNum()\' class=button>&nbsp;&nbsp;
 <input type=button value=\'Há»§y bá»�\'  onclick=\'javascript: hideDialog()\' class=button>
 </center>
 </form>
 </center>";
 return $response;

 }*/

function fillNote($manv, $hoten_tv, $ghichu, $from, $to, $yr, $mon, $d, $curPage, $report_type, $ten)
{

	global $connectionstring;

	$note = $ghichu;
	$note = strFormat($note);
	$note = str_replace("\\","\\\\",$note);
	$note = str_replace("<br />","\n",$note);
	$note = str_replace("'","\'",$note);
	$note = htmlspecialchars($note);

	$response = "";
	$response .= "<center>Ghi chÃº nhÃ¢n viÃªn <b>$hoten_tv</b></center>";
	$response .= "<br/><form name=cmt action=main_details.php>";
	$response .= "<center><textarea name=ghichu cols=35 rows=5>$note</textarea></center><br>";
	$response .= "<input type=hidden name=yr value=$yr >";
	$response .= "<input type=hidden name=ten value=$ten >";
	$response .= "<input type=hidden name=mon value=$mon>";
	$response .= "<input type=hidden name=d value=$d >";
	$response .= "<input type=hidden name=from value=$from >";
	$response .= "<input type=hidden name=to value=$to >";
	$response .= "<input type=hidden name=manv value=$manv >";
	$response .= "<input type=hidden name=curPage value=$curPage >";
	$response .= "<input type=hidden name=report_type value=$report_type >";
	$response .= "<center><input type=submit value=\'Ghi chÃº\' class=button>&nbsp;&nbsp;<input type=button value=\'Há»§y bá»�\'  onclick=\'javascript: hideDialog()\' class=button></center>";

	$response .= "</form>";

	return $response;
}

function strFormat($str)
{
	$str = str_replace("\r\n","<br />",$str);
	$str = addslashes($str);

	return $str;
}

if (isset($ghichu))
{
	$query = "UPDATE nhanvien SET ghichu = '$ghichu' WHERE manv = '$manv'";
	$queryexe = odbc_do($connectionstring, $query);


	//HÃ m URL_forward cÃ³ tÃ¡c dá»¥ng lÆ°u láº¡i trang hiá»‡n táº¡i (dÃ¹ng trong viá»‡c phÃ¢n trang)
	URL_forward("main.php?report_type=$report_type&from=$from&to=$to&yr=$yr&mon=$mon&d=$d&view=1&curPage=$curPage&gh=1&manvupdate=$manv&ten=$ten", "2");
}

/**
 *
 * Enter description here ...
 */
function addChangePasswordListener(){
	$matkhau = $_POST['pwd1'];
	$matvv = $_SESSION['matvv'];
	if (isset($_POST['changePass']) && $_POST['changePass'] != "") {
		global $connectionstring;

		$query = "UPDATE tvv SET matkhau = md5('$matkhau') WHERE matvv = '$matvv'";
		mysql_query($query, $connectionstring);
		//HÃ m URL_forward cÃ³ tÃ¡c dá»¥ng lÆ°u láº¡i trang hiá»‡n táº¡i (dÃ¹ng trong viá»‡c phÃ¢n trang)
		URL_forward("/Main.php?changePassReq=success&matkhau=$matkhau", "10");
		exit();
	}
}
/**
 * Delete Truage Register
 */
function addDeleteTruageListener(){
	if (isset($_GET['action']) && $_GET['action'] == "deleteTruageReg") {
		global $connectionstring;
		$matvv = $_SESSION['matvv'];
		$makh = $_GET['makh'];
		$date = $_REQUEST['date'];

		$querykh = " DELETE FROM luotdo WHERE matvv = '$matvv' AND makh = '$makh'";
		mysql_query($querykh, $connectionstring);

		$queryLuotdo = " DELETE FROM khachhang WHERE makh = '$makh'";
		mysql_query($queryLuotdo, $connectionstring);

		URL_forward("/Main.php?deleteTruageReg=success&view=1&date=$date", "10");
		exit();
	}
}

/**
 *
 * Enter description here ...
 */
function addRegisterTruageListener(){
	global $connectionstring;
	$matvv = $_SESSION['matvv'];
	$hoten = $_POST['kh_hoten'];
	$diachi = $_POST['kh_diachi'];
	$sodt = $_POST['kh_dt'];
	$email = $_POST['kh_email'];
	$cmnd = $_POST['kh_cmnd'];
	$namsinh = $_POST['kh_namsinh'];
	$ngaydo = $_POST['ngaydo'];
	// TODO: do the check here
	if (isset($_POST['registerTruage']) && $_POST['registerTruage'] != "") {
		if (preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $ngaydo)) {
			$ngaydo = formatToDbDate($ngaydo, "Y-m-d");
		}
		$date = date_format(date_create($ngaydo), "Ymd");

		$maxNoRegisterCond = array("ngaydo" => $ngaydo, "matvv" => $matvv);
		$maxNoRegisterCount = getTableResultCount("luotdo", $maxNoRegisterCond, $connectionstring);
		$maxNoRegisterEachDay = getFieldFromTable("dieukien", "dktoidanguoi", $connectionstring);

		if ($maxNoRegisterCount >= $maxNoRegisterEachDay) {
			URL_forward("/Main.php?registerInvalid=excceedMaxNoDay&numOfDay=$maxNoRegisterCount", "5");
			exit();
		}

		global $connectionstring;

		// TODO: Check condition
		$query = "INSERT INTO khachhang(hoten, diachi, sodt, email, cmnd, namsinh) VALUES ('$hoten', '$diachi', '$sodt', '$email', '$cmnd', '$namsinh')";
		$queryexe = mysql_query($query, $connectionstring);

		$makh = mysql_insert_id($connectionstring);

		$queryReg = "INSERT INTO luotdo(matvv, makh, ngaydo) VALUES ('$matvv', '$makh', '$ngaydo')";
		$queryRegExe = mysql_query($queryReg, $connectionstring);

		URL_forward("/Main.php?dkTruageReg=success&hoten=$hoten&view=1&date=$date", "5");
		exit();
	}
}

function checkRegisterLimit($matvv, $ngaydo){

}

?>

